//
// Created by DengLibin on 2018/12/13 0013.
//
#include <iostream>
#include <string>
#include <map>
using namespace std;

void myMap()
{
    map<int, string> m;

    //4种方式插入元素
    //方式1（返回一个pair，迭代器-bool）
    pair<map<int,string>::iterator,bool> result =m.insert(pair<int,string>(1,"student1"));
    cout << result.first->first << "--->" << result.first->second << endl;
    cout << (result.second ? "插入成功" : "插入失败") << endl;

    //方式2
    pair<map<int, string>::iterator, bool> result2 = m.insert(make_pair(2, "student2"));

    //方式3
    pair<map<int, string>::iterator, bool> result3 = m.insert(map<int, string>::value_type(3, "student3"));
    pair<map<int, string>::iterator, bool> result4 = m.insert(map<int, string>::value_type(3, "student33"));//插入失败
    //方式4，该方法非常直观，但存在一个性能的问题。插入3时，先在mapStu中查找主键为3的项，若没发现，则将一个键为3，
    //值为初始化值的对组插入到mapStu中，然后再将值修改成“student4”。若发现已存在3这个键，则修改这个键对应的value
    m[4] = "student4";
    for (map<int, string>::iterator it = m.begin(); it != m.end(); it++)
    {
        cout << it->first << "->" << it->second << endl;
    }

    //删除元素
    //删除元素
    while (!m.empty())
    {
        m.erase(m.begin());
    }
    cout << "map大小:" << m.size() << endl;//0
}
//查找
void findMap()
{
    map<int, string> m;
    for (int i = 0; i < 10; i++)
    {
        m.insert(pair<int, string>(i, "teacher" + 1));
    }
    //查找，返回迭代器
    map<int, string>::iterator it = m.find(9);
    if (it == m.end())
    {
        cout << "失败" << endl;
    }
    else
    {
        cout << "找到9 "<<it->first << "--->" << it->second << endl;
    }

    //返回两个迭代器,查找 键>=5的两个迭代器
    pair<map<int, string>::iterator, map<int, string>::iterator> p = m.equal_range(5);
    if (p.first == m.end())
    {
        cout << "=5的键不存在" << endl;
    }
    else
    {
        cout << p.first->first << "--->" << p.first->second << endl;
    }
    if (p.second == m.end())
    {
        cout << ">5的键不存在" << endl;
    }
    else
    {
        cout << p.second->first << "--->" << p.second->second << endl;
    }

}
class Person
{
public:
    string name;
    int age;
    Person(string name, int age)
    {
        this->name = name;
        this->age = age;
    }
    bool operator==(Person &p2)
    {
        return (this->name == p2.name && this->age == p2.age);
    }
};
//multimap 一个键可以对应多个值
void my_multimap()
{
    Person p1("张三", 23);
    Person p2("李四", 34);
    Person p3("王五", 25);
    multimap<string, Person> m;
    m.insert(make_pair("研发部", p1));
    m.insert(pair<string, Person>("研发部", p2));
    m.insert(multimap<string, Person>::value_type("销售部", p3));
    //遍历
    for (multimap<string, Person>::iterator it = m.begin(); it != m.end(); it++)
    {
        cout << it->first << "---" << it->second.age << "---" << it->second.name << endl;
    }
    //查找
    multimap<string, Person>::iterator it = m.find("销售部");
    int num = m.count("销售部");//键对应的值的个数
    while (it != m.end() && num > 0) {
        cout << it->first << "-->" << it->second.age << "--" << it->second.name << endl;
        it++;
        num--;
    }

    //修改
    for (multimap<string, Person>::iterator it = m.begin(); it != m.end(); it++)
    {
        if (it->second.name == "张三")
        {
            it->second.name = "陈六";
        }
    }
    for (multimap<string, Person>::iterator it = m.begin(); it != m.end(); it++)
    {
        cout << it->first << "---" << it->second.age << "---" << it->second.name << endl;
    }
}
int main29(int argc, char* argv[])
{
    //myMap();
    //findMap();
   my_multimap();
    return 0;
}

